package org.wu.framework.lazy.orm.database.sql.expand.database.persistence.factory;

import org.wu.framework.lazy.orm.core.config.LazyOperationAttribute;
import org.wu.framework.lazy.orm.core.config.prop.DefaultLazyDataSourceAttribute;
import org.wu.framework.lazy.orm.core.config.prop.LazyDataSourceAttribute;
import org.wu.framework.lazy.orm.database.lambda.LazyBaseDDLOperation;
import org.wu.framework.lazy.orm.database.lambda.dynamic.LazyDynamicDatasourceAdapter;
import org.wu.framework.lazy.orm.database.lambda.dynamic.factory.LazyDynamicAdapterFactory;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.LazyOperationParameter;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.ddl.*;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.interceptor.SqlInterceptorAdapter;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.proxy.LazyDDLOperationProxy;

import javax.sql.DataSource;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * LazyDDLOperationProxy 代理工厂
 */
public final class LazyOperationDDLProxyFactory {


    /**
     * 创建默认的操作代理对象
     *
     * @param sourceProperties 数据库链接配置
     * @return
     */
    public static LazyDDLOperationProxy createDefaultLazyDDLOperationProxy(LazyDataSourceAttribute sourceProperties) {
        return createDefaultLazyDDLOperationProxy(sourceProperties, null);
    }



    /**
     * 创建默认的操作代理对象
     *
     * @param sourceProperties    数据库链接配置
     * @param lazyOperationAttribute 操作配置
     * @return
     */
    public static LazyDDLOperationProxy createDefaultLazyDDLOperationProxy(LazyDataSourceAttribute sourceProperties,
                                                                           LazyOperationAttribute lazyOperationAttribute) {
        SqlInterceptorAdapter defaultSqlInterceptorAdapter = SqlInterceptorAdapterFactory.createDefaultSqlInterceptorAdapter();
        LazyOperationParameter lazyOperationParameter =
                new LazyOperationParameter()
                        .setLazyOperationAttribute(lazyOperationAttribute)
                        .setSqlInterceptorAdapter(defaultSqlInterceptorAdapter);
        List<LazyDDLOperationMethod> lazyOperationMethods = Arrays.asList(
                new LazyOperationMethodCreateTable(lazyOperationParameter),
                new LazyOperationMethodCreateTableEndpoint(lazyOperationParameter),
//                new LazyOperationMethodExecute(),
//                new LazyOperationMethodExecuteOne(),
//                new LazyOperationMethodExecuteSQL(lazyOperationAttribute),
//                new LazyOperationMethodExecuteSQLForBean(lazyOperationAttribute),
                new LazyOperationMethodPerfect(lazyOperationParameter),
                new LazyOperationMethodUpdateTable(lazyOperationParameter),
                new LazyOperationMethodUpdateTableEndpoint(lazyOperationParameter),
                new LazyOperationMethodStringScriptRunner(lazyOperationParameter),
                new LazyOperationMethodScriptRunner(lazyOperationParameter),
                new LazyOperationMethodStringScriptRunner(lazyOperationParameter)
        );
        return createLazyDDLOperationProxy(null, sourceProperties, lazyOperationMethods);
    }
    /**
     * 创建默认的操作代理对象
     *
     * @param dataSourceMap 数据库链接配置
     * @return LazyDDLOperationProxy
     */
    public static LazyDDLOperationProxy createDefaultLazyDDLOperationProxy(Map<String,DataSource> dataSourceMap ) {
        return createDefaultLazyDDLOperationProxy(dataSourceMap, null);
    }    /**
     * 创建默认的操作代理对象
     *
     * @param dataSourceMap    数据库链接配置
     * @param lazyOperationAttribute 操作配置
     * @return
     */
    public static LazyDDLOperationProxy createDefaultLazyDDLOperationProxy(Map<String,DataSource> dataSourceMap,
                                                                           LazyOperationAttribute lazyOperationAttribute) {
        SqlInterceptorAdapter defaultSqlInterceptorAdapter = SqlInterceptorAdapterFactory.createDefaultSqlInterceptorAdapter();
        LazyOperationParameter lazyOperationParameter =
                new LazyOperationParameter()
                        .setLazyOperationAttribute(lazyOperationAttribute)
                        .setSqlInterceptorAdapter(defaultSqlInterceptorAdapter);
        List<LazyDDLOperationMethod> lazyOperationMethods = Arrays.asList(
                new LazyOperationMethodCreateTable(lazyOperationParameter),
                new LazyOperationMethodCreateTableEndpoint(lazyOperationParameter),
                new LazyOperationMethodPerfect(lazyOperationParameter),
                new LazyOperationMethodUpdateTable(lazyOperationParameter),
                new LazyOperationMethodUpdateTableEndpoint(lazyOperationParameter),
                new LazyOperationMethodStringScriptRunner(lazyOperationParameter),
                new LazyOperationMethodScriptRunner(lazyOperationParameter),
                new LazyOperationMethodStringScriptRunner(lazyOperationParameter)
        );
        return createLazyDDLOperationProxy(dataSourceMap, null, lazyOperationMethods);
    }

    /**
     * 创建默认的操作代理对象
     *
     * @param sourceProperties     数据库链接配置
     * @param lazyOperationMethods 操作的方法
     * @return
     */
    public static LazyDDLOperationProxy createLazyDDLOperationProxy(LazyDataSourceAttribute sourceProperties,
                                                                    List<LazyDDLOperationMethod> lazyOperationMethods) {
        return createLazyDDLOperationProxy(null, sourceProperties, lazyOperationMethods);
    }

    /**
     * 创建默认的操作代理对象
     *
     * @param sourceProperties     数据库链接配置
     * @param lazyOperationMethods 操作的方法
     * @param dataSourceMap        上下文 数据源
     * @return
     */
    public static LazyDDLOperationProxy createLazyDDLOperationProxy(Map<String, DataSource> dataSourceMap,
                                                                    LazyDataSourceAttribute sourceProperties,
                                                                    List<LazyDDLOperationMethod> lazyOperationMethods
    ) {
        LazyDynamicDatasourceAdapter lazyDynamicAdapter = LazyDynamicAdapterFactory.createLazyDynamicAdapter(dataSourceMap, sourceProperties);
        return new LazyDDLOperationProxy(lazyOperationMethods, lazyDynamicAdapter);
    }


    /**
     * 创建对象操作
     *
     * @param lazyDataSourceAttribute 数据库链接配置
     * @return
     */
    public static LazyBaseDDLOperation createLazyDDLOperation(LazyDataSourceAttribute lazyDataSourceAttribute) {
        LazyDDLOperationProxy defaultLazyOperationProxy = createDefaultLazyDDLOperationProxy(lazyDataSourceAttribute);
        return createLazyDDLOperation(defaultLazyOperationProxy);
    }

    /**
     * 创建对象操作
     *
     * @param dataSourceMap 数据库链接配置
     * @return LazyBaseDDLOperation
     */
    public static LazyBaseDDLOperation createLazyDDLOperation(Map<String,DataSource> dataSourceMap ) {
        LazyDDLOperationProxy defaultLazyOperationProxy = createDefaultLazyDDLOperationProxy(dataSourceMap);
        return createLazyDDLOperation(defaultLazyOperationProxy);
    }

    /**
     * 创建对象操作
     *
     * @param lazyDDLOperationProxy 操作对象代理对象
     * @return
     */
    public static LazyBaseDDLOperation createLazyDDLOperation(LazyDDLOperationProxy lazyDDLOperationProxy) {
        return (LazyBaseDDLOperation) Proxy.newProxyInstance(LazyBaseDDLOperation.class.getClassLoader(), new Class[]{LazyBaseDDLOperation.class}, lazyDDLOperationProxy);
    }

    /**
     * 创建 LazySqlOperation 对象
     *
     * @param url      数据库地址
     * @param username 用户名称
     * @param password 用户密码
     * @return 自动填充数据对象
     * @return
     */
    public static LazyBaseDDLOperation createLazyDDLOperation(String url, String username, String password) {
        LazyDataSourceAttribute lazyDataSourceAttribute = new DefaultLazyDataSourceAttribute();
        lazyDataSourceAttribute.setUrl(url);
        lazyDataSourceAttribute.setUsername(username);
        lazyDataSourceAttribute.setPassword(password);
        return createLazyDDLOperation(lazyDataSourceAttribute);
    }

    /**
     * 创建 LazySqlOperation 对象
     *
     * @param host     数据库IP
     * @param port     数据库端口
     * @param username 用户名称
     * @param password 用户密码
     * @return 自动填充数据对象
     * @return
     */
    public static LazyBaseDDLOperation createLazyDDLOperation(String host, int port, String schema, String username, String password) {
        String urlFormat = "jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf-8&useSSL=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai";
        String url = String.format(urlFormat, host, port, schema);

        return createLazyDDLOperation(url, username, password);
    }


}
